package com.woniuxy.house.provider;

import com.woniuxy.house.entity.Post;
import com.woniuxy.house.entity.Source;
import com.woniuxy.house.entity.User;
import org.apache.ibatis.jdbc.SQL;

public class PostSqlProvider {
    public static String update(Post post){
        String sql= new SQL(){
            {
                UPDATE("house_post");
                if(post.getTitle()!=null&&!post.getTitle().equals("")){
                    SET("title=#{title}");
                }
                if(post.getIsAcross()!=null&&!post.getIsAcross().equals("")){
                    SET("isacross=#{isAcross}");
                }if(post.getIsDelete()!=null&&!post.getIsDelete().equals("")){
                SET("isdelete=#{isDelete}");
            }
                WHERE("id=#{id}");

            }

        }.toString();
        return sql;
    }
    public String getByCondition(Post post) {
        return  new SQL() {
            {
                SELECT("*");
                FROM("house_post");
                if(post.getTitle()!=null&&!post.getTitle().equals("")){
                    WHERE("title like concat('%',#{title},'%')");
                }
                if(post.getPostDetail()!=null&&!post.getPostDetail().equals("")){
                    WHERE("post_detail_id=#{postDetail.id}");
                }
                if(post.getIsDelete()!=null&&!post.getIsDelete().equals("")){
                    WHERE("isdelete=#{isdelete}");
                }
                if(post.getIsAcross()!=null&&!post.getIsAcross().equals("")){
                    WHERE("isacross=#{isacross}");
                }
                if(post.getId()!=null){
                    WHERE("id=#{id}");
                }

                if(post.getSign()!=null&&!post.getSign().equals("")){
                    WHERE("sign= #{sign}");
                }
                if(post.getTime()!=null&&!post.getTime().equals("")){
                    WHERE("time= #{time}");
                }
                if(post.getUser()!=null){
                    WHERE("user_id=#{user.id}");
                }

            }
        }.toString();
    }
}
